package com.example.customview.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.example.customview.R
import com.example.customview.util.getBitmap
import com.sample.base.utils.dp

@SuppressLint("NewApi")
class MultiLineTextView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    companion object {
        private val IMAGE_SIZE = 150.dp
        private val IMAGE_PADDING = 50.dp
        private val TEXT_SIZE = 20.dp
        private val TEXT_SIZE_LEFT_PADDING = 6.dp
        private val TEXT_CONTENT =
            "Lorem ipsum dolor sit amet, " +
                    "consectetur adipiscing elit, sed do eiusmod tempor incididunt ut " +
                    "consectetur adipiscing elit, sed do eiusmod tempor incididunt ut " +
                    "consectetur adipiscing elit, sed do eiusmod tempor incididunt ut " +
                    "labore et dolore magna aliqua. Id volutpat lacus laoreet non curabitur " +
                    "gravida arcu ac tortor. Scelerisque eu ultrices vitae auctor eu augue." +
                    "gravida arcu ac tortor. Scelerisque eu ultrices vitae auctor eu augue." +
                    " Bibendum enim facilisis gravida neque convallis a cras. Aliquam purus sit amet luctus."
    }

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        textSize = TEXT_SIZE.toFloat()
    }

    private val fontMetrics = Paint.FontMetrics()


    override fun onDraw(canvas: Canvas) {
        //draw bitmap
        val bitmap = this.getBitmap(IMAGE_SIZE, R.drawable.avatar)
        canvas.drawBitmap(
            bitmap,
            (width - IMAGE_SIZE - TEXT_SIZE_LEFT_PADDING).toFloat(),
            IMAGE_PADDING.toFloat(),
            paint
        )

        // draw text
        paint.color = resources.getColor(R.color.colorWhite, resources.newTheme())
        paint.getFontMetrics(fontMetrics)
        var measuredWidth = floatArrayOf(0f)
        var start = 0
        var count = 0
        var verticalOffset = -fontMetrics.top
        var lineMaxWidth = 0f
        while (start < TEXT_CONTENT.length) {
            lineMaxWidth = if (verticalOffset + fontMetrics.bottom < IMAGE_PADDING.toFloat()
                || verticalOffset + fontMetrics.top > (IMAGE_SIZE + IMAGE_PADDING).toFloat()
            ) {
                width.toFloat() - TEXT_SIZE_LEFT_PADDING - TEXT_SIZE_LEFT_PADDING
            } else {
                width.toFloat() - IMAGE_SIZE - TEXT_SIZE_LEFT_PADDING - TEXT_SIZE_LEFT_PADDING
            }
            count = paint.breakText(
                TEXT_CONTENT,
                start,
                TEXT_CONTENT.length,
                true,
                lineMaxWidth,
                measuredWidth
            )
            canvas.drawText(
                TEXT_CONTENT,
                start,
                start + count,
                TEXT_SIZE_LEFT_PADDING.toFloat(),
                verticalOffset,
                paint
            )
            start += count
            verticalOffset += paint.fontSpacing
        }

    }
}